﻿using BMSQ.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BMSQ.Admin.Goods
{
    public partial class Modify : PageBase
    {
        private string vGoodsImage
        {
            get
            {
                if (ViewState["vGoodsImage"] == null)
                {
                    return "";
                }
                else
                {
                    return ViewState["vGoodsImage"].ToString();
                }
            }
            set
            {
                ViewState["vGoodsImage"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGoodsType1();

                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    string Goods_ID = Request.Params["id"];
                    ShowInfo(Goods_ID);
                }
            }
        }

        protected void ddlGoodsType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGoodsType1.SelectedIndex > 1)
            {
                BindGoodsType2();
            }
        }

        private void BindGoodsType1()
        {
            BindDrowDownList(ddlGoodsType1, new BLL.Goods_Type1().GetModelList(""), "GT1_Name", "GT1_ID", true);
        }

        private void BindGoodsType2()
        {
            BindDrowDownList(ddlGoodsType2, new BLL.Goods_Type2().GetModelList(string.Format(" GT1_ID='{0}'", ddlGoodsType1.SelectedValue)), "GT2_Name", "GT2_ID", true);
        }

        private void ShowInfo(string Goods_ID)
        {
            BLL.Goods_List bll = new BLL.Goods_List();
            Model.Goods_List model = bll.GetModel(Goods_ID);
            this.lblGoods_ID.Text = model.Goods_ID;
            this.ddlGoodsType1.SelectedValue = model.GT1_ID;

            BindGoodsType2();

            this.ddlGoodsType2.SelectedValue = model.GT2_ID;
            this.txtGoods_Name.Text = model.Goods_Name;
            vGoodsImage = model.Goods_Image;
            imgGoods.ImageUrl = Common.GoodsImageFolder() + "/" + model.Goods_Image;
            this.txtInfo.Value = model.Goods_Description;
            this.txtGoods_Unit.Text = model.Goods_Unit;

        }

        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";
            if (this.ddlGoodsType1.SelectedIndex < 1)
            {
                strErr += "GT1_ID不能为空！\\n";
            }
            if (this.ddlGoodsType2.SelectedIndex < 1)
            {
                strErr += "GT2_ID不能为空！\\n";
            }
            if (this.txtGoods_Name.Text.Trim().Length == 0)
            {
                strErr += "Goods_Name不能为空！\\n";
            }
            if (this.txtGoods_Unit.Text.Trim().Length == 0)
            {
                strErr += "Goods_Unit不能为空！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string Goods_ID = this.lblGoods_ID.Text;
            string GT1_ID = this.ddlGoodsType1.SelectedValue;
            string GT2_ID = this.ddlGoodsType2.SelectedValue;
            string GT3_ID = "";
            string GT4_ID = "";
            string Goods_Name = this.txtGoods_Name.Text;
            string Goods_Image = vGoodsImage;
            string Goods_Description = this.txtInfo.Value;
            string Goods_Unit = this.txtGoods_Unit.Text;


            Model.Goods_List model = new BLL.Goods_List().GetModel(Goods_ID);
            model.Goods_ID = Goods_ID;
            model.GT1_ID = GT1_ID;
            model.GT2_ID = GT2_ID;
            model.GT3_ID = GT3_ID;
            model.GT4_ID = GT4_ID;
            model.Goods_Name = Goods_Name;
            model.Goods_Image = Goods_Image;
            model.Goods_Description = Goods_Description;
            model.Goods_Unit = Goods_Unit;

            BLL.Goods_List bll = new BLL.Goods_List();
            bll.Update(model);
            MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }

        protected void btnUploadImage_Click(object sender, EventArgs e)
        {
            if (imgUpload.PostedFile.ContentLength > 0)
            {
                if (imgUpload.PostedFile.ContentLength > Common.MAXIMAGESIZE)
                {
                    MessageBox.Show(this, string.Format("APP端显示的图片不能大于{0}K", Common.MAXIMAGESIZE / 1024));
                }
                else
                {
                    if (Common.CheckAPPImageExtension(Path.GetExtension(imgUpload.FileName)))
                    {
                        UploadFiles up = new UploadFiles();
                        vGoodsImage = up.UploadImg(imgUpload.PostedFile, Common.GoodsImageFolder());

                        imgGoods.ImageUrl = Common.GoodsImageFolder() + "/" + vGoodsImage;
                    }
                    else
                    {
                        MessageBox.Show(this, string.Format("图片后缀错误，标准的图片格式应该是{0}", Common.APPImageExtension()));
                    }
                }
            }
        }
    }
}